package com.fq.modules.sys.role.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fq.api.api.ApiRest;
import com.fq.api.api.controller.BaseController;
import com.fq.modules.sys.role.dto.QueryRoleDTO;
import com.fq.modules.sys.role.dto.SysRoleDTO;
import com.fq.modules.sys.role.dto.SysRoleMenuReqDTO;
import com.fq.modules.sys.role.entity.SysRole;
import com.fq.modules.sys.role.service.SysRoleMenuService;
import com.fq.modules.sys.role.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 超chao
 * @Description 系统角色控制器
 * @Date 2024/10/23/周三 10:18
 * @Version 1.0
 */
@Api(tags = {"系统角色"})
@RestController
@RequestMapping("/api/sys/role")
public class SysRoleController extends BaseController {
    @Autowired
    private SysRoleService baseService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    /**
     * 分页查询--角色列表
     */
    @RequiresPermissions(value = {"sys:role:list"})
    @ApiOperation(value = "分页查询--角色列表")
    @RequestMapping(value = "/paging", method = { RequestMethod.GET})
    public ApiRest<IPage<SysRoleDTO>> paging(@Validated @ModelAttribute QueryRoleDTO reqDTO) {

        //分页查询并转换
        IPage<SysRoleDTO> page = baseService.paging(reqDTO);
        return super.success(page);
    }

    /**
     * 增改--角色
     */
    @RequiresPermissions(value = {"sys:role:save","sys:role:update"},logical = Logical.OR)
    @ApiOperation(value = "增改--角色")
    @RequestMapping(value = "/save", method = { RequestMethod.POST})
    public ApiRest<String> save(@Validated @RequestBody SysRoleDTO reqDTO) {
        baseService.save(reqDTO);
        return super.success(reqDTO.getId());
    }

    /**
     * 删除--角色
     */
    @RequiresPermissions(value = {"sys:role:delete"})
    @ApiOperation(value = "删除--角色")
    @RequestMapping(value = "/delete", method = { RequestMethod.DELETE})
    public ApiRest<Void> delete(@RequestParam(value = "id") String id) {
        //根据ID删除
        baseService.deleteById(id);
        return super.success();
    }

    /**
     * 查询--角色详情
     */
    @ApiOperation(value = "查询--角色详情")
    @RequestMapping(value = "/detail", method = { RequestMethod.GET})
    public ApiRest<SysRoleDTO> find(@RequestParam(value = "id") Long id) {
        SysRole entity = baseService.getById(id);
        SysRoleDTO dto = new SysRoleDTO();
        BeanUtils.copyProperties(entity, dto);
        return super.success(dto);
    }

    /**
     * 查询--角色菜单授权列表
     */
    @RequiresPermissions(value = {"sys:role:grant"})
    @ApiOperation(value = "查询--角色菜单授权列表")
    @RequestMapping(value = "/list-menus", method = { RequestMethod.GET})
    public ApiRest<List<Long>> listMenus(@RequestParam(value = "roleId") String roleId) {

        List<Long> ids = sysRoleMenuService.findRoleMenus(roleId);
        return super.success(ids);
    }

    /**
     * 增改--角色菜单授权
     */
    @RequiresPermissions(value = {"sys:role:grant"})
    @ApiOperation(value = "增改--角色菜单授权")
    @RequestMapping(value = "/save-menus", method = { RequestMethod.POST})
    public ApiRest<Void> saveMenus(@Validated @RequestBody SysRoleMenuReqDTO reqDTO) {

        // 保存授权
        sysRoleMenuService.saveRoleIds(reqDTO.getRoleId(), reqDTO.getMenuIds());
        return super.success();
    }
    /**
     * 启用或禁用角色
     */
    @RequiresPermissions(value = {"sys:role:state"})
    @ApiOperation(value = "更新--启用或禁用角色")
    @RequestMapping(value = "/change-state", method = { RequestMethod.PUT})
    public ApiRest<Void> changeState(@Validated @RequestBody com.fq.common.core.api.dto.BaseChangeStateDTO<String> reqDTO) {

        // 更新状态
        baseService.updateRoleState(reqDTO);
        return super.success();
    }
}
